package org.example.ui.employee;

import org.example.business.OrderBusiness;
import org.example.manage.OrderManage;
import org.example.model.Order;
import org.example.model.OrderItem;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class ManageOrderItem extends JPanel{
    private JLabel[] labels = new JLabel[5];
    private JButton[] buttons = new JButton[2];
    private SpringLayout layout = new SpringLayout();
    private JCheckBox box;
    private JScrollPane scrollPane;
    private Order order;
    public ManageOrderItem(Order order, int n) {
        setLayout(layout);
        this.order = order;
        List<Order> list = new OrderManage("data/order_file/need_do_order.json").read();
        Font font = new Font("", Font.BOLD, 15);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        String[] milkStr = {String.valueOf(n), order.mailbox, String.valueOf(order.total),
                String.valueOf(order.totalPrice), order.condition};
        JTable table = new JTable();
        TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
        box = (JCheckBox) renderer;
        Image image2 = new ImageIcon("data/background/bag_icon.jpg").getImage().
                getScaledInstance(10, 10, Image.SCALE_DEFAULT);
        box.setIcon(new ImageIcon(image2));
        box.setSelectedIcon(new ImageIcon(image2));
        add(box);
        setProduct(order.orderItems);
        add(scrollPane);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        layout.putConstraint(SpringLayout.WEST, box, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, box, 40, SpringLayout.NORTH, this);
        for (int i = 0; i < 5; i++) {
            labels[i] = new JLabel(milkStr[i], JLabel.CENTER);
            labels[i].setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
            labels[i].setFont(font);
            add(labels[i]);
            if (i == 0) {
                layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, box);
            }
            else {
                layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, labels[i-1]);
            }
            if (i == 2) {
                layout.putConstraint(SpringLayout.WEST,scrollPane,0,SpringLayout.EAST,labels[i-1]);
                layout.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.SOUTH, scrollPane, 100, SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.WEST, labels[i], 0, SpringLayout.EAST, scrollPane);

            }
            layout.putConstraint(SpringLayout.NORTH, labels[i], 0, SpringLayout.NORTH, this);
            layout.putConstraint(SpringLayout.SOUTH, labels[i],0, SpringLayout.SOUTH, this);
        }
        layout.putConstraint(SpringLayout.EAST, labels[0], 100, SpringLayout.EAST, box);
        layout.putConstraint(SpringLayout.EAST, labels[1], 180, SpringLayout.EAST, labels[0]);
        layout.putConstraint(SpringLayout.EAST,scrollPane,230,SpringLayout.EAST,labels[1]);
        layout.putConstraint(SpringLayout.EAST, labels[2], 100, SpringLayout.EAST, scrollPane);
        layout.putConstraint(SpringLayout.EAST, labels[3], 100, SpringLayout.EAST, labels[2]);
        layout.putConstraint(SpringLayout.EAST, labels[4], 100, SpringLayout.EAST, labels[3]);
        String[] operate = {"发货", "退款"};
        for (int i = 0; i < 2; i++) {
            buttons[i] = new JButton(operate[i]);
            add(buttons[i]);
            if (i == 0){
                layout.putConstraint(SpringLayout.WEST, buttons[i], 9, SpringLayout.EAST, labels[4]);

            }
            else {
                layout.putConstraint(SpringLayout.WEST, buttons[i], 5, SpringLayout.EAST, buttons[i-1]);
            }
            layout.putConstraint(SpringLayout.NORTH, buttons[i], 35, SpringLayout.NORTH, this);
        }
        setAllListener();
        setVisible(true);
    }
    private void setAllListener() {
        buttons[0].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new OrderManage("data/order_file/need_do_order.json").move(order);
                Order newOrder = new Order(order, "已完成");
                new OrderManage("data/order_file/" + order.mailbox + ".json").update(order, newOrder);
                ManageOrderPanel.setAdminItems();
                OrderBusiness.updateOrders();
                JOptionPane.showMessageDialog(ManageOrderItem.this, "已发货");
            }
        });
        buttons[1].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new OrderManage("data/order_file/need_do_order.json").move(order);
                Order newOrder = new Order(order, "已取消");
                new OrderManage("data/order_file/" + order.mailbox + ".json").update(order, newOrder);
                ManageOrderPanel.setAdminItems();
                OrderBusiness.updateOrders();
                JOptionPane.showMessageDialog(ManageOrderItem.this, "已退款");
            }
        });
    }
    public void setProduct(List<OrderItem> lists) {
        JList jList = new JList();
        DefaultListModel<OrderItem> model = new DefaultListModel<>();
        model.addAll(lists);
        jList.setModel(model);
        jList.setCellRenderer(new Item());
        scrollPane = new JScrollPane(jList);
    }
    public class Item implements ListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = new JLabel();
            label.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
            OrderItem item = (OrderItem) value;
            String str = "<html>" + item.milkName + "<br>" + item.number + " x ￥" +
                    item.price + "<br>" +item.specification + "</html>";
            Image image = new ImageIcon(item.imagePath).getImage().
                    getScaledInstance(50, 50, Image.SCALE_DEFAULT);
            label.setIcon(new ImageIcon(image));
            label.setText(str);
            return label;
        }
    }
}
